
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// </editor-fold>

public class Mail {

// <editor-fold defaultstate="collapsed" desc="init strings">
    public static final Logger log = LoggerFactory.getLogger(Mail.class);

    private final static String VERSION = "Mail v5.0 2009-08-24";
    public final static String LINE = "\n**************************************************";

    public final static String CD_MESSAGE500 =
        "You have chosen Collect on Delivery (C.O.D.) as your payment method and we have a requirement for COD orders that "+
        "exceed $500.00 to be paid by Money Order and is subject for our credit department approval.\n" +
        "Our representative will contact you to verify the payment method.\nWe apologize for any inconvenience this may cause.\n";

    public final static String CD_MESSAGE100 =
        "You have chosen Collect on Delivery (C.O.D.) as your payment method and we have a requirement for COD orders that exceed $100.00 "+
        "to be paid by Cashiers Check or Money Order unless our credit department approved a Company Check payment prior to the purchase.\n" +
        "Our representative will contact you to verify the payment method.\nWe apologize for any inconvenience this may cause.\n";

    public final static String MESSAGE1 =
        "Please review your order information in case you need to make any changes. If you have any questions about your order, " +
        "please visit the http://www.compuvest.com/info/CustomerService.jsp " +
        "section of the web site to get to the right department so that the issue was quickly resolved.\n" +
        "We hope you have had an excellent and rewarding experience shopping with us.\n" +
        "Please click here to rate our services " +
        "https://www.pricegrabber.com/rating_merchrev.php?retid=17\n\n";

    public final static String POLICY =
        "\n\nOrder Acceptance Policy:\n" +
        "Your submission of an order and/or receipt of an order confirmation, automated or otherwise, does not signify our acceptance " +
        "of such order. CompuVest reserves the right to cancel any order that we deem necessary, at our sole discretion, regardless of " +
        "the order status or whether you have received a confirmation of the order. We reserve the right to require additional " +
        "information from you before accepting or processing any order.\n\n";

    public final static String BUTTOM =
        "Thank you\n" +
        "CompuVest Corp.\n" +
        "Phone: (425) 738-4091\n" +
        "Fax  : (425) 251-5000\n" +
        "info@compuvest.com\n" +
        "www.compuvest.com\n\n";

    public final static String resMessage =
        "Thank you for your interest in our reseller program. Your application has been submitted and will be reviewed by one of our " +
        "account managers.  We will be contacting you within 24 hours if any questions arise and will help you take full advantage of " +
        "this program. You will also be assigned a designated sales person to work directly with your company - ensuring everything " +
        "is  handled smoothly and accurately.\nIf you have applied for Net terms or COD, qualification may take longer - depending " +
        "on how fast your bank and trades respond.\n" +
        "For resellers who would like to apply for COD or NET terms, please fill out the application form http://www.compuvest.com/img/form/reseller.rtf " +
        "and fax to (425) 251-5000.\n\n\n" +
        "We appreciate your business!\n\n" +
        "Sales Team\nCompuvest.com reseller division\n" +
        "3600 Lind Ave SW, suite 130\n" +
        "Renton, WA 98057\n" +
        "Tel: (425) 738-4091\n" +
        "Fax: (425) 251-5000\n" +
        "b2b@compuvest.com\n" +
        "www.compuvest.com\n";

    public final static String EMAIL_CUSTOMER_HEAD =
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" +
        "<html><head><META http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>" +
        "<div style='font-weight:bold;'>" +
        "<fieldset>" +
        "<div style='background-color:white;'>" +
        "<table style='width:100%;margin:2px 2px;'>" +
        "<tr>" +
        "<td colspan='2'>" +
        "<img src='http://www.compuvest.com/img/hd/hlogo.png' alt='Compuvest Corp.' title=''/>" +
        "<div>&nbsp;&nbsp;Compuvest.com&copy; Corporation, 3600 Lind Ave. S.W, Suite 130, Renton, WA 98057 USA.</div><br/>" +

        "<div>&nbsp;&nbsp;Dear Customer please make sure we have the correct address for your order. UPS and FedEx is charging  $11.00 " +
        "&nbsp;&nbsp;fee to you for all incorrect addresses. Zip codes and lack of Apt. numbers " +
        "&nbsp;&nbsp;are the most common cause for this fee.</div><br/>" +

        "<div>&nbsp;&nbsp;Tel: (425) 738-4091</div>" +
        "<div>&nbsp;&nbsp;Fax: (425) 251-5000</div>";

    public final static String EMAIL_CUSTOMER_BOTTOM =
        "<div style='text-align:right;'>" +
        "<font size='2'>Thank you for your Order.</font>&nbsp;&nbsp;&nbsp;&nbsp;" +
        "</div><br/>" +

        "</td>" +
        "</tr>" +

        "<tr><td colspan='2' style='text-align:center;vertical-align:middle'>" +
        "<img src='http://dealofthedeal.com/images/follow.jpg' alt='' title=''>" +
        "<a href='http://twitter.com/#!/CompuvestCorp' style='border:0 none;'>" +
        "<img src='http://www.dealofthedeals.com/images/twitter-logo.jpg' title='' alt='Follow us on Twitter.' style='border:0 none;' width='60px'></a>" +
        "<a href='http://www.facebook.com/pages/Compuvest-Corporation/137501466319430?sk=wall' style='border:0 none;' target='_blank'>" +
        "<img src='http://www.dealofthedeals.com/images/facebook-logo.gif' title='' alt='Follow us on FaceBook.' style='border:0 none;' width='60px'></a>" +
        "</td></tr>" +

        "</table>" +

        "</div>" +
        "</fieldset>" +
        "</div>" +
        "</body></html>";

// </editor-fold>

    private DecimalFormat priceDF = new DecimalFormat("$#,##0.00");
    private GoogleEmail gmail;

// <editor-fold defaultstate="collapsed" desc="Mail()">
    public Mail() {

        gmail = new GoogleEmail();

        log.info("Init: " + VERSION);
    }
// </editor-fold>

    public void sendEmail(Basket basket, String stat) {

        sendToCompany(basket, stat);
        sendToOlga(basket, stat);
        sendToCustomerHTML(basket);
    }

    public void sendReseller(Customer cust) {

        toReseller(cust);
        toCompany(cust);
    }

// <editor-fold defaultstate="collapsed" desc="sendToOlga()">

    public void sendToOlga(Basket basket, String stat) {

        Customer cust = basket.getCustomer();
        StringBuilder subject = new StringBuilder(100);
        StringBuilder body = new StringBuilder(10000);
        StringBuilder shipBody = new StringBuilder(1000);

        subject.append(basket.getReseller() ? " -=Reseller=- " : "").append("Order From Compuvest.com #").append(basket.getOrderNumber());

        body.append("Confirmation Number: ").append(basket.getOrderNumber());
        body.append(basket.getReseller() ? " -=Reseller=- " : "").append(basket.getBlindShipping() ? "-=Blind=-" : "");
        body.append("\n\nItemNo\t\tQty\tPrice\t\tAmount\tItem Name");

        for(ItemsInBasket item : basket.getContents()) {
            body.append("\n").append(item.getAccwareCode());
            body.append("\t").append(item.getTotalQuantity());
            body.append("\t").append(priceDF.format(item.getSellPrice()));
            body.append("\t").append(priceDF.format(item.getTotalQuantity() * item.getSellPrice()));
            body.append("\t").append(item.getItemName());
            body.append(item.getPromotionPriceString(true));

            if(item.getWarrantyPrice() != 0d) {
                body.append("\n***warranty:\t").append(item.getWarrantyDesc()).append("\t");
                body.append(priceDF.format(item.getWarrantyTotal())).append(" for #").append(item.getAccwareCode());
            }
        }
	if(basket.getPromotion() != null) {
            body.append("\n").append(basket.getCouponMessage(false));
	}
        body.append("\nTotal Amount:\t\t").append(priceDF.format(basket.getTotal(true)));

        shipBody.append("\n\nShip Company:\t\t").append(cust.getShipping().getCompany());
        shipBody.append("\nShip Name:\t\t\t").append(cust.getShipping().getName());
        shipBody.append("\nShip Address:\t\t").append(cust.getShipping().getAddress1()).append(" ").append(cust.getShipping().getAddress2());
        shipBody.append("\n\t\t\t\t").append(cust.getShipping().getZip()).append(" ").append(cust.getShipping().getZip4());
        shipBody.append(" ").append(cust.getShipping().getCity()).append(", ").append(cust.getShipping().getState());
        shipBody.append(" ").append(cust.getShipping().getCountry());
        shipBody.append("\nShip Phone:\t\t\t").append(cust.getShipping().getPhone());
        if(!cust.getShipping().getCell().isEmpty()) {
            shipBody.append("\nShip Cell:\t\t\t").append(cust.getShipping().getCell());
        }
        if(!cust.getShipping().getFax().isEmpty()) {
            shipBody.append("\nShip Fax:\t\t\t").append(cust.getShipping().getFax());
        }
        shipBody.append("\nEmail:\t\t\t").append(cust.getEmail());

        body.append("\n\nShipping:\t\t\t").append(priceDF.format(basket.getShippingCost())).append(" ").append(basket.getShippingDescription());
	if(basket.getSignatureRequired()) {
            body.append("\nSignature Required:\t").append(priceDF.format(basket.getSignaturePrice()));
	}
        if(basket.getTaxCost() != 0d) {
            body.append("\nWA Tax:\t\t\t").append(priceDF.format(basket.getTaxCost()));
        }

	if ("PP".equals(basket.getPaymentMethod())) { // PAYPAL
            subject.append(" PayPal");

            body.append("\n\nOrder Total:\t\t\t").append(priceDF.format(basket.getGrandTotal()));

            body.append(shipBody);

            body.append("\nPayment Method:\t\tPAYPAL");
        }
	else { // not ED, WT, UN

            body.append("\n\nOrder Total:\t\t\t").append(priceDF.format(basket.getGrandTotal()));

            body.append(shipBody);

            body.append("\n\nName on CC:\t\t").append(cust.getCName());
            body.append("\nBilling Company:\t\t").append(cust.getBilling().getCompany());
            body.append("\nBilling name:\t\t").append(cust.getBilling().getName());
            body.append("\nBilling Address:\t\t").append(cust.getBilling().getAddress1()).append(" ").append(cust.getBilling().getAddress2());
            body.append("\n\t\t\t\t").append(cust.getBilling().getZip()).append(" ").append(cust.getBilling().getZip4());
            body.append(" ").append(cust.getBilling().getCity()).append(", ").append(cust.getBilling().getState());
            body.append(" ").append(cust.getBilling().getCountry());
            body.append("\nBilling Phone:\t\t").append(cust.getBilling().getPhone());
            if(!cust.getBilling().getCell().isEmpty()) {
                body.append("\nBilling Cell:\t\t\t").append(cust.getBilling().getCell());
            }
            if(!cust.getBilling().getFax().isEmpty()) {
                body.append("\nBilling Fax:\t\t\t").append(cust.getBilling().getFax());
            }
            if(basket.getNotes().trim().length() != 0) {
                body.append("\nNotes:\t\t").append(basket.getNotes());
            }
            if(cust.getShippingAccount().length() != 0) {
                body.append("\nUse Cust Acc#:\t").append(cust.getShippingAccount());
            }

            if(basket.getPaymentMethod().equals("CC")) { // credit card
                subject.append(" Credit Card");

                body.append("\n\nCC Number:\t\t").append(cust.getCardNumber());
                body.append("\nCC Exp. Date:\t\t").append(cust.getCExpDate());
                body.append("\nCC Type:\t\t\t").append(cust.getCType());
                body.append("\nCC Verify Number:\t").append(cust.getCNumberAdd());
                body.append("\nBank Phone:\t\t").append(cust.getBankPhone());
            }
            else if(basket.getPaymentMethod().equals("CD")) { // by Check
                subject.append(" By Check");

                body.append("\n\nDriver License Number:\t").append(cust.getLicenseNumber());
                body.append("\nDriver License State:\t").append(cust.getLicenseState());
                body.append("\nCheck Number:\t\t").append(cust.getCheckNumber());
                body.append("\nCheck Account Number:\t").append(cust.getCheckAccountNumber());
                body.append("\nCheck Route Number:\t").append(cust.getCheckRoutingNumber());
            }

            body.append("\n\nICS Reply:\t").append(stat);
        }

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "ordercredit@compuvest.com" },
            subject.toString(),
            body.toString(),
            "text/plain"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendToCompany()">

    public void sendToCompany(Basket basket, String stat) {

	Customer cust = basket.getCustomer();
	StringBuilder subject = new StringBuilder();
	StringBuilder body = new StringBuilder();
	StringBuilder shipBody = new StringBuilder();

	subject.append(basket.getReseller() ? " -=Reseller=- " : "");

	body.append("\nConfirmation Number: <b>").append(basket.getOrderNumber());
	body.append(basket.getReseller() ? " -=Reseller=- " : "").append(basket.getBlindShipping() ? "-=Blind=-" : "").append("</b>");

	body.append("<table width='100%' border='1'>");
	body.append("<tr><th>ItemNo</th><th>Qty</th><th>Price</th><th>Amount</th><th>Item Name</th></tr>");

	for(ItemsInBasket item : basket.getContents()) {
            body.append("<tr>");
            body.append("<td>").append(item.getAccwareCode()).append("</td>");
            body.append("<td>").append(item.getTotalQuantity()).append("</td>");
            body.append("<td>").append(priceDF.format(item.getSellPrice())).append("</td>");
            body.append("<td>").append(priceDF.format(item.getTotalQuantity() * item.getSellPrice())).append("</td>");
            body.append("<td>").append(item.getItemName()).append("<br/>").append(item.getCondition()).append(' ').append(item.getPackage());
            body.append("<br/>").append(item.getPromotionPriceString(true)).append("</td>");
            body.append("</tr>");

            if(item.getWarrantyPrice() != 0d) {
		body.append("<tr><td colspan='5'>Extended warranty: ").append(item.getWarrantyDesc()).append(" ");
		body.append(priceDF.format(item.getWarrantyTotal())).append(" for #").append(item.getAccwareCode()).append("</td></tr>");
            }
	}
	if(basket.getSignatureRequired()) {
            body.append("<tr><td colspan='5'>Signature Required : ").append(priceDF.format(basket.getSignaturePrice())).append("</td></tr>");
	}
	body.append("</table>");

	shipBody.append("<tr><td colspan='2'><hr/></td></tr>");
	shipBody.append("<tr><td colspan='2'>Shipping</td>");
	shipBody.append("<tr><td>Company:</td><td>").append(cust.getShipping().getCompany()).append("</td></tr>");
	shipBody.append("<tr><td>Name:</td><td>").append(cust.getShipping().getName()).append("</td></tr>");
	shipBody.append("<tr><td>Address:</td><td>").append(cust.getShipping().getAddress1()).append(' ').append(cust.getShipping().getAddress2()).append("</td></tr>");
	shipBody.append("<tr><td></td><td>").append(cust.getShipping().getZip()).append(' ').append(cust.getShipping().getZip4());
	shipBody.append(' ').append(cust.getShipping().getCity()).append(", ").append(cust.getShipping().getState());
	shipBody.append(' ').append(cust.getShipping().getCountry()).append("</td></tr>");
	shipBody.append("<tr><td>Phone:</td><td>").append(cust.getShipping().getPhone()).append("</td></tr>");
	if(!cust.getShipping().getCell().isEmpty()) {
            shipBody.append("<tr><td>Cell:</td><td>").append(cust.getShipping().getCell()).append("</td></tr>");
	}
	if(!cust.getShipping().getFax().isEmpty()) {
            shipBody.append("<tr><td>Fax:</td><td>").append(cust.getShipping().getFax()).append("</td></tr>");
	}
	body.append("<tr><td>Email:</td><td>").append(cust.getEmail()).append("</td></tr>");

	body.append("<table>");
	body.append("<tr><td>Shipping: ").append(basket.getShippingDescription()).append(":</td><td>").append(priceDF.format(basket.getShippingCost())).append("</td></tr>");
	if(basket.getTaxCost() != 0d) {
            body.append("<tr><td>WA Tax:</td><td>").append(priceDF.format(basket.getTaxCost())).append("</td></tr>");
	}

	if("CD".equals(basket.getPaymentMethod())) { // telecheck
            subject.append("By Check order From Compuvest.com ").append(basket.getOrderNumber());

            body.append("<tr><td>Total Amount:</td><td>").append(priceDF.format(basket.getGrandTotal())).append("</td></tr>");

            body.append(shipBody);

            body.append("</table>");
	}
	else if("PP".equals(basket.getPaymentMethod())) { // PAYPAL
            subject.append("PayPal order From Compuvest.com ").append(basket.getOrderNumber());

            body.append("<tr><td>Total Amount:</td><td>").append(priceDF.format(basket.getGrandTotal())).append("</td></tr>");

            body.append(shipBody);

            body.append("<tr><td>Payment Method:</td><td>PayPal</td></tr>");
            body.append("</table>");
	}
	else { // not C.O.D
            subject.append(basket.getPaymentMethod()).append(" Order From Compuvest.com ").append(basket.getOrderNumber());

            body.append("<tr><td>Total Amount:</td><td>").append(priceDF.format(basket.getGrandTotal())).append("</td></tr>");

            body.append(shipBody);

            body.append("<tr><td colspan='2'><hr></td></tr>");
            body.append("<tr><td colspan='2'>Billing</td>");
            body.append("<tr><td>Name on CC:</td><td>").append(cust.getCName()).append("</td></tr>");
            body.append("<tr><td>Company:</td><td>").append(cust.getBilling().getCompany()).append("</td></tr>");
            body.append("<tr><td>Name:</td><td>").append(cust.getBilling().getName()).append("</td></tr>");
            body.append("<tr><td>Address:</td><td>").append(cust.getBilling().getAddress1()).append(' ').append(cust.getBilling().getAddress2()).append("</td></tr>");
            body.append("<tr><td></td><td>").append(cust.getBilling().getZip()).append(" ").append(cust.getBilling().getZip4());
            body.append(' ').append(cust.getBilling().getCity()).append(", ").append(cust.getBilling().getState());
            body.append(' ').append(cust.getBilling().getCountry()).append("</td></tr>");
            body.append("<tr><td>Phone:</td><td>").append(cust.getBilling().getPhone()).append("</td></tr>");
            if(!cust.getBilling().getCell().isEmpty()) {
		body.append("<tr><td>Cell:</td><td>").append(cust.getBilling().getCell()).append("</td></tr>");
            }
            if(!cust.getBilling().getFax().isEmpty()) {
		body.append("<tr><td>Fax:</td><td>").append(cust.getBilling().getFax()).append("</td></tr>");
            }

            if(basket.getNotes().trim().length() == 0) {
		body.append("\n");
            }
            else {
		body.append("<tr><td>Notes:</td><td>").append(basket.getNotes()).append("</td></tr>");
            }

//	    body.append("<tr><td>CC Number:</td><td>").append(cust.getCardNumber()).append("</td></tr>");
            body.append("<tr><td>CC Exp. Date:</td><td>").append(cust.getCExpDate()).append("</td></tr>");
            body.append("<tr><td>CC Type:</td><td>").append(cust.getCType()).append("</td></tr>");
//	    body.append("<tr><td>CC Verify Number:</td><td>").append(cust.getCNumberAdd()).append("</td></tr>");
            body.append("<tr><td>Bank Phone:</td><td>").append(cust.getBankPhone()).append("</td></tr>");
            body.append("</table>");
            body.append("<hr/>");

            body.append("<table>");
            body.append("<tr><td>ICS Reply:</td><td>").append(stat).append(")</td></tr>");
            body.append("</table>");
	}

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "orderinfo@compuvest.com" },
            subject.toString(),
            body.toString(),
            "text/html"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendToCustomerHTML()">

    public void sendToCustomerHTML(Basket basket) {

        Customer cust = basket.getCustomer();
        Items basketItems = new Items(); // for getResellerPriceLine()

        StringBuilder subject = new StringBuilder();
        StringBuilder body = new StringBuilder();

        subject.append(basket.getReseller() ? "Reseller " : "").append("Order from www.compuvest.com #").append(basket.getOrderNumber());

        body.append(EMAIL_CUSTOMER_HEAD);

        body.append("<div style='text-align:right;'>").append(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date())).append("&nbsp;&nbsp;&nbsp;</div>");
        if(basket.getReseller()) {
            body.append("<div style='text-align:left;'>&nbsp;&nbsp;Reseller ID: ").append(cust.getResellerCode()).append("</div>");
        }
        body.append("<hr/>");
        body.append("<div style='text-align:center;font-size:16px;'>Order <span style='color:red;'>").append(basket.getOrderNumber()).append("</span> Confirmation</div>");
        body.append("<br/>");
        body.append("</td>");
        body.append("</tr>");

        body.append("<tr>");
        body.append("<td style='vertical-align:top;width:50%;'>");
        body.append("<div style='padding:2px 6px;'>Billing Address<br/>");
        body.append("<span style='font-weight:normal;'>");
        body.append(cust.getBilling().getCompany()).append("<br/>");
        body.append(cust.getBilling().getName()).append("<br/>");
        body.append(cust.getBilling().getAddress1()).append(' ');
        body.append(cust.getBilling().getAddress2()).append(' ');
        body.append(cust.getBilling().getCity()).append(',');
        body.append(cust.getBilling().getState()).append(' ');
        body.append(cust.getBilling().getZip()).append(' ');
        body.append(cust.getBilling().getZip4()).append(' ');
        body.append(cust.getBilling().getCountry()).append("<br/>");
        body.append("Tel: ").append(cust.getBilling().getPhone()).append("<br/>");
        if(!cust.getBilling().getCell().isEmpty()) {
            body.append("Cell: ").append(cust.getBilling().getCell()).append("<br/>");
        }
        if(!cust.getBilling().getFax().isEmpty()) {
            body.append("Fax: ").append(cust.getBilling().getFax());
        }
        body.append("</span></div>");
        body.append("</td>");
        body.append("<td style='vertical-align:top;width:50%;'>");
        body.append("<div style='padding:2px 6px;'>").append(basket.getBlindShipping() ? "<span style='color:red;'>Blind</span> " : "").append("Shipping Address:<br/>");
        body.append("<span style='font-weight:normal;'>");
        body.append(cust.getShipping().getCompany()).append("<br/>");
        body.append(cust.getShipping().getName()).append("<br/>");
        body.append(cust.getShipping().getAddress1()).append(' ');
        body.append(cust.getShipping().getAddress2()).append(' ');
        body.append(cust.getShipping().getCity()).append(",");
        body.append(cust.getShipping().getState()).append(' ');
        body.append(cust.getShipping().getZip()).append(' ');
        body.append(cust.getShipping().getZip4()).append(' ');
        body.append(cust.getShipping().getCountry()).append("<br/>");
        body.append("Tel: ").append(cust.getShipping().getPhone()).append("<br/>");
        if(!cust.getShipping().getCell().isEmpty()) {
            body.append("Cell: ").append(cust.getShipping().getCell()).append("<br/>");
        }
        if(!cust.getShipping().getFax().isEmpty()) {
            body.append("Fax: ").append(cust.getShipping().getFax());
        }
        body.append("</span></div>");
        body.append("</td>");
        body.append("</tr>");

        body.append("<tr>");
        body.append("<td colspan='2'><br/>");
        body.append("<table style='width:99%;border-color:#d4d4d4;font-weight:bold;font-size:8pt;color:#353c1a;line-height:11px;text-decoration:none;border-collapse:collapse;' border='1'>");
        body.append("<colgroup><col width='11%'/><col width='53%'/><col width='6%'/><col span='3' width='10%'/></colgroup>");
        body.append("<tr style='background-color:silver;' height='30'><th>Part #</th><th>Product Name</th><th>Qty</th><th>Price</th><th>Extended Warranty</th><th>Amount</th></tr>");

        boolean ground = false;
        for(ItemsInBasket item : basket.getContents()) {

            body.append("<tr align='center' height='30' bgcolor='#ffff").append((ground = !ground) ? "df" : "ff").append("'>");
            body.append("<td id='nowrap'>").append(item.getAccwareCode()).append("<div style='font-size:10px;color:blue;'>").append(item.getCondition()).append("</div></td>");
            body.append("<td align='left'><div class='tlink'>").append(item.getItemName()).append("</div>");
            body.append(basket.getReseller() ? basketItems.getResellerPriceLine(item) : "");
            body.append(item.getPromotionPriceString(false));
            body.append("</td>");

            body.append("<td>").append(item.getTotalQuantity()).append("</td>");
            body.append("<td>");
            if(basket.getReseller() && item.getResPrice1() != 0d) {
                body.append("<div style='color:#674a36;text-decoration:line-through;'>").append(priceDF.format(item.getPrice())).append("</div>");
            }
            body.append(priceDF.format(item.getSellPrice()));
            body.append("</td>");
            body.append("<td>").append(item.getWarrantyTotal() == 0d ? "" : priceDF.format(item.getWarrantyTotal()) + "<div style='font-size:10px;color:green;'>" + item.getWarrantyDesc() + "</div>").append("</td>");
            body.append("<td>").append(priceDF.format(item.getSellPrice() * item.getTotalQuantity() + item.getWarrantyPrice() * item.getTotalQuantity())).append("</td>");
            body.append("</tr>");
        }

        body.append("<tr><td colspan='6' align='right' style='padding:8px 10px;'>");
        if(basket.getCouponMessage(false).length() > 0) {
            body.append("<p style='color:red;'>").append(basket.getCouponMessage(false)).append("</p>");
        }
        body.append("Total Amount : ").append(priceDF.format(basket.getTotal(true))).append("</td></tr>");
        body.append("</table><br/>\n");

        body.append("<div style='text-align:center;font-size:16px;'>Summary</div>");
        body.append("<table style='width:99%;border-color:#d4d4d4;font-weight:bold;font-size:8pt;color:#353c1a;line-height:11px;text-decoration:none;border-collapse:collapse;' border='1'>");
        body.append("<tr style='background-color:silver;' height='30'><th width='60%'>&nbsp;</th><th width='20%'>&nbsp;</th><th width='20%'>&nbsp;</th></tr>");

        if(cust.getShipping().getState().equals("WA")) {
            body.append("<tr height='30'><td>WA Tax</td><td>Tax</td><td align='right'>").append(priceDF.format(basket.getTaxCost())).append("</td></tr>");
        }
        if(basket.getSignatureRequired()) {
            body.append("<tr height='30'><td>Signature Required</td><td>Charge</td><td align='right'>").append(priceDF.format(basket.getSignaturePrice())).append("</td></tr>");
        }

        body.append("<tr height='30'>");
        body.append("<td>Shipping Method: ").append(basket.getShippingDescription()).append("</td>");
        body.append("<td>Shipping</td>");
        body.append("<td align='right'>").append(priceDF.format(basket.getShippingCost())).append("</td>");
        body.append("</tr>");

        body.append("<tr height='30'>");
        body.append("<td colspan='2' align='left'>Order Total : <div id='am'>&nbsp;</div></td>");
        body.append("<td align='right' valign='bottom'>").append(priceDF.format(basket.getGrandTotal())).append("</td>");
        body.append("</tr>");
        body.append("</table>");

        body.append("<hr/>");
        body.append("<div style='padding:2px 6px 2px 6px;font-weight:normal;'>").append(getMessageForOrder(basket)).append("</div>");//################
        body.append("<br/>");

        body.append(EMAIL_CUSTOMER_BOTTOM);

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { cust.getEmail() },
            subject.toString(),
            body.toString(),
            "text/html"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getMessageForOrder()">

    private String getMessageForOrder(Basket basket) {

	StringBuilder body = new StringBuilder();

        // coupon message
	if(basket.getCoupon() != null) {
            body.append("<p>As a new customer you were awarded with coupon <b>CV").append(basket.getCoupon().getCode());
            body.append("</b> valued at <b>$").append(basket.getCoupon().getPrice()).append("</b><br/>");
            body.append("You may use this coupon  with any purchase during the next ").append(basket.getCoupon().getDays()).append(" days.<br/>");
            body.append("Just enter this number in the coupon field in your shopping basket at time of checkout.<br/>");
            body.append("Coupon expiration date is <b>").append(new SimpleDateFormat("MM/dd/yyyy").format(basket.getCoupon().getValidDate())).append("</b></p>");
	}
        body.append("<p>This message is to inform you that we have received your on-line order and that your order is currently in process.</p>");
//        if("CD".equals(basket.getPaymentMethod())) { // not use now
//            if(basket.getGrandTotal() > 500d) {
//                body.append("<p>You have chosen Collect on Delivery (C.O.D.) as your payment method and we have a requirement for COD orders that exceed ");
//                body.append("<b>$500.00</b> to be paid by Money Order and is subject for our credit department approval.");
//                body.append("Our representative will contact you to verify the payment method.<br/>We apologize for any inconvenience this may cause.</p>");
//            }
//            else if(basket.getGrandTotal() > 100d) {
//                body.append("<p>You have chosen Collect on Delivery (C.O.D.) as your payment method and we have a requirement for COD orders that exceed ");
//                body.append("<b>$100.00</b> to be paid by Cashiers Check or Money Order unless our credit department approved a Company Check payment prior to the purchase.");
//                body.append("Our representative will contact you to verify the payment method.<br/>We apologize for any inconvenience this may cause.</p>");
//            }
//        }

        body.append("<p>Please review your order information in case you need to make any changes. If you have any questions about your order, ");
        body.append("please visit the <a href='http://www.compuvest.com/info/CustomerService.jsp'>Customer Service</a> ");
        body.append("section of the web site to get to the right department so that the issue was quickly resolved. ");
        body.append("We hope you have had an excellent and rewarding experience shopping with us. ");
        body.append("Please click here to rate our services ");
        body.append("<a href='https://www.pricegrabber.com/rating_merchrev.php?retid=17' target='_blank'>Price Grabber</a></p>");

        body.append("<p>Order Acceptance Policy:<br/>");
        body.append("Your submission of an order and/or receipt of an order confirmation, automated or otherwise, does not signify our acceptance ");
        body.append("of such order. CompuVest reserves the right to cancel any order that we deem necessary, at our sole discretion, regardless of ");
        body.append("the order status or whether you have received a confirmation of the order. We reserve the right to require additional ");
        body.append("information from you before accepting or processing any order.</p>");

	return body.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendPassword()">

    public void sendPassword(Customer cust) {

	StringBuilder body = new StringBuilder(1000);
	body.append("\nDear ").append(cust.getBilling().getName()).append(",\n\n");
	body.append("Your password is: \'").append(cust.getPassword()).append("\'\n\n");
	body.append("If you still experiencing technical problems\n");
	body.append("please contact our customer service department.\n\n\n");
	body.append(BUTTOM);

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { cust.getEmail() },
            "Password request from Compuvest.com " + new Date(),
            body.toString(),
            "text/plain"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toCompany()">

    public void toCompany(Customer cust) {

	StringBuilder body = new StringBuilder();
	body.append("\nNew reseller").append("\n\n");
	body.append("\nResseler Code:\t").append(cust.getResellerCode());
	body.append("\nEmail:\t\t").append(cust.getEmail()).append("\n");
	body.append("\nBilling Company:\t").append(cust.getBilling().getCompany());
	body.append("\nName:\t\t").append(cust.getBilling().getName());
	body.append("\nAddress:\t").append(cust.getBilling().getAddress1());
	body.append("\nCity:\t\t").append(cust.getBilling().getCity());
	body.append("\nState:\t").append(cust.getBilling().getState());
	body.append("\nZip:\t\t").append(cust.getBilling().getZip());
	body.append("\nCountry:\t").append(cust.getBilling().getCountry());
	body.append("\nPhone:\t").append(cust.getBilling().getPhone());
	body.append("\nCell:\t\t").append(cust.getBilling().getCell());
	body.append("\nFax:\t\t").append(cust.getBilling().getFax());
	body.append("\n\n");

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "reseller@compuvest.com" },
            "Registrated Reseller from Compuvest.com " + new Date(),
            body.toString(),
            "text/plain"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toReseller()">

    public void toReseller(Customer cust) {

	StringBuilder body = new StringBuilder(6000);
	body.append("\nDear ").append(cust.getBilling().getName()).append(",\n\n");
	body.append(resMessage);

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { cust.getEmail(), "megan@compuvest.com" },
            "Registration Reseller request from Compuvest.com " + new Date(),
            body.toString(),
            "text/plain"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendEmailToLostCustomerDetail()">

    public void sendEmailToLostCustomerDetail(Basket basket) {

        String body = customerDetail(basket);

        Customer cust = basket.getCustomer();

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "rest@compuvest.com" },
            "Compuvest Corp. Detail Lost Customer " + cust.getEmail(),
            body.toString(),
            "text/html"
        );
    }
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc="customerDetail()">

    private String customerDetail(Basket basket) {

        Customer cust = basket.getCustomer();

        StringBuilder body = new StringBuilder();
        body.append("\n\n<br/>ipaddress : ").append(basket.getCompuvestCookies().getIPAddress()).append("\n<br/>");
        body.append("email : ").append(cust.getEmail()).append("\n<br/>");

        if(basket.notEmpty()) {

            body.append("<table border='1'>");
            body.append("<tr><th>ItemNo</th><th>Qty</th><th>Price</th><th>Amount</th><th>Item Name</th></tr>");

            for(ItemsInBasket item : basket.getContents()) {
                body.append("<tr>");
                body.append("<td>").append(item.getAccwareCode()).append("</td>");
                body.append("<td>").append(item.getTotalQuantity()).append("</td>");
                body.append("<td>").append(priceDF.format(item.getSellPrice())).append("</td>");
                body.append("<td>").append(priceDF.format(item.getTotalQuantity() * item.getSellPrice())).append("</td>");
                body.append("<td>").append(item.getItemName()).append("<br/>").append(item.getCondition()).append(' ').append(item.getPackage());
                body.append("<br/>").append(item.getPromotionPriceString(true)).append("</td>");
                body.append("</tr>");

                if(item.getWarrantyPrice() != 0d) {
                    body.append("<tr><td colspan='5'>Extended warranty: ").append(item.getWarrantyDesc()).append(" ");
                    body.append(priceDF.format(item.getWarrantyTotal())).append(" for #").append(item.getAccwareCode()).append("</td></tr>");
                }
                body.append("<tr>");
                body.append("<td colspan='5'><a href='http://10.0.2.18:8189/Admin/promotion/EmailPromotionCode.jsp?email=");
                body.append(cust.getEmail()).append("&iid=").append(item.getItemID()).append("'>Generate Promotion Code</a>");
                body.append("</td>");
                body.append("</tr>");
            }

            body.append("<tr><td colspan='2'><hr/></td></tr>");
            body.append("<tr><td colspan='2'>Shipping</td>");
            body.append("<tr><td>Ship Company:</td><td>").append(cust.getShipping().getCompany()).append("</td></tr>");
            body.append("<tr><td>Name:</td><td>").append(cust.getShipping().getName()).append("</td></tr>");
            body.append("<tr><td>Address:</td><td>").append(cust.getShipping().getAddress1()).append(' ').append(cust.getShipping().getAddress2()).append("</td></tr>");
            body.append("<tr><td></td><td>").append(cust.getShipping().getZip()).append(' ').append(cust.getShipping().getZip4());
            body.append(' ').append(cust.getShipping().getCity()).append(", ").append(cust.getShipping().getState());
            body.append(' ').append(cust.getShipping().getCountry()).append("</td></tr>");
            body.append("<tr><td>Phone:</td><td>").append(cust.getShipping().getPhone()).append("</td></tr>");
            if(!cust.getShipping().getCell().isEmpty()) {
                body.append("<tr><td>Cell:</td><td>").append(cust.getShipping().getCell()).append("</td></tr>");
            }
            if(!cust.getShipping().getFax().isEmpty()) {
                body.append("<tr><td>Fax:</td><td>").append(cust.getShipping().getFax()).append("</td></tr>");
            }
            body.append("<tr><td colspan='2'><hr/></td></tr>");
            body.append("<tr><td colspan='2'>Billing</td>");
            body.append("<tr><td>Name on CC:</td><td>").append(cust.getCName()).append("</td></tr>");
            body.append("<tr><td>Company:</td><td>").append(cust.getBilling().getCompany()).append("</td></tr>");
            body.append("<tr><td>Name:</td><td>").append(cust.getBilling().getName()).append("</td></tr>");
            body.append("<tr><td>Address:</td><td>").append(cust.getBilling().getAddress1()).append(" ").append(cust.getBilling().getAddress2()).append("</td></tr>");
            body.append("<tr><td></td><td>").append(cust.getBilling().getZip()).append(' ').append(cust.getBilling().getZip4());
            body.append(' ').append(cust.getBilling().getCity()).append(", ").append(cust.getBilling().getState());
            body.append(' ').append(cust.getBilling().getCountry()).append("</td></tr>");
            body.append("<tr><td>Phone:</td><td>").append(cust.getBilling().getPhone()).append("</td></tr>");
            if(!cust.getBilling().getCell().isEmpty()) {
                body.append("<tr><td>Cell:</td><td>").append(cust.getBilling().getCell()).append("</td></tr>");
            }
            if(!cust.getBilling().getFax().isEmpty()) {
                body.append("<tr><td>Fax:</td><td>").append(cust.getBilling().getFax()).append("</td></tr>");
            }

            if(basket.getNotes().trim().length() == 0) {
                body.append("\n");
            }
            else {
                body.append("<tr><td>Notes:</td><td>").append(basket.getNotes()).append("</td></tr>");
            }

            body.append("</table>");
        }

        return body.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendDropPrice()">

    public void sendDropPrice(String itemID, String name, String phone, String email) {

        if(itemID == null && email == null) { return; }

	StringBuilder body = new StringBuilder(200);
	body.append("<table border='1' bgcolor='white'>");
	body.append("<tr><td>Customer name: </td><td>").append(name != null ? name : "").append("</td></tr>");
	body.append("<tr><td>Phone: </td><td>").append(phone != null ? phone : "").append("</td></tr>");
	body.append("<tr><td>Email: </td><td>").append(email).append("</td></tr>");
        body.append("<tr><td colspan='2' align='center'><a href='http://10.0.2.18:8189/Admin/promotion/EmailDropPrice.jsp?email=").append(email);
	body.append("&iid=").append(itemID);
	body.append("'>Send promo code.</a></td></tr>");
	body.append("</table>");

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "rest@compuvest.com" },
            "Compuvest Corp. Drop Price Alert " + email,
            body.toString(),
            "text/html"
        );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="quoteRequest()">

    public void quoteRequest(String message) {

        gmail.send(
            "webinfo@compuvest.com",
            new String[] { "marat@compuvest.com" },
            "Compuvest Corp. Quote Request ",
            message,
            "text/html"
        );
    }
// </editor-fold>

}
